package com.buanalintas.sandals.pos.returshopdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.sandals.pos.entities.PosMasterItemStock;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosMasterStatus;
import com.buanalintas.sandals.pos.entities.PosTransactionReturShopDetail;
import com.buanalintas.sandals.pos.item.Item;
import com.buanalintas.sandals.pos.returshopdetail.AddSize;
import com.buanalintas.sandals.pos.status.Status;
import javax.persistence.EntityManager;

public class ReturShopDetail extends Page {

    private Field returShopDetailId;
    private Field returShopDetailReturShopId;
    private Field returShopDetailItemId;
    private Field returShopDetailStatusId;
    private Field returShopDetailSizeId;
    private Field returShopDetailSize;
    private Field returShopDetailQuantity;
    private Field returShopDetailEstimatedDate;
    private Field returShopDetailDescription;

    public ReturShopDetail() {
        super();
        setTheClass(PosTransactionReturShopDetail.class);

        returShopDetailId = new Field();
        returShopDetailId.setName("returShopDetailId");
        returShopDetailId.setCaption("id");
        returShopDetailId.setListProperties("");
        returShopDetailId.setInputProperties("");
        getFields().add(returShopDetailId);

        returShopDetailReturShopId = new Field();
        returShopDetailReturShopId.setName("returShopDetailReturShopId");
        returShopDetailReturShopId.setCaption("retur id");
        returShopDetailReturShopId.setListProperties("");
        returShopDetailReturShopId.setInputProperties("");
        getFields().add(returShopDetailReturShopId);

        returShopDetailItemId = new Field();
        returShopDetailItemId.setName("returShopDetailItemId");
        returShopDetailItemId.setCaption("item");
        returShopDetailItemId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        returShopDetailItemId.setInputProperties(" size=\"25\"");
        returShopDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        returShopDetailItemId.getSelectorColumns().add(new String[]{"itemModel", " Model", "width=\"90%\""});
        returShopDetailItemId.setSelectorOrder("t.itemCode");
        returShopDetailItemId.setRequired(true);
        getFields().add(returShopDetailItemId);
        
        returShopDetailSize = new Field();
        returShopDetailSize.setName("returShopDetailSize");
        returShopDetailSize.setCaption("size");
        returShopDetailSize.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        returShopDetailSize.setEntityRelation(PosMasterSize.class);
        returShopDetailSize.setInputType(Field.INPUT_CHECK_BOX);
        returShopDetailSize.setRequired(true);
        getFields().add(returShopDetailSize);

        returShopDetailSizeId = new Field();
        returShopDetailSizeId.setName("returShopDetailSizeId");
        returShopDetailSizeId.setCaption("size");
        returShopDetailSizeId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
//        returShopDetailSizeId.setEntityRelation(PosMasterSize.class);
        returShopDetailSizeId.setRequired(true);
        getFields().add(returShopDetailSizeId);

        returShopDetailQuantity = new Field();
        returShopDetailQuantity.setName("returShopDetailQuantity");
        returShopDetailQuantity.setCaption("quantity");
        returShopDetailQuantity.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        returShopDetailQuantity.setInputProperties(" size=\"5\"");
        returShopDetailQuantity.setRequired(true);
        getFields().add(returShopDetailQuantity);

        returShopDetailEstimatedDate = new Field();
        returShopDetailEstimatedDate.setName("returShopDetailEstimatedDate");
        returShopDetailEstimatedDate.setCaption("estimated date");
        returShopDetailEstimatedDate.setListProperties(" align=\"center\" width=\"5%\" nowrap");
        returShopDetailEstimatedDate.setInputProperties(" size=\"8\"");
        returShopDetailEstimatedDate.setRequired(true);
        getFields().add(returShopDetailEstimatedDate);

        returShopDetailStatusId = new Field();
        returShopDetailStatusId.setName("returShopDetailStatusId");
        returShopDetailStatusId.setCaption("status");
        returShopDetailStatusId.setListProperties(" align=\"left\" width=\"5%\" nowrap");
        returShopDetailStatusId.setInputProperties("");
        returShopDetailStatusId.setInputType(Field.INPUT_COMBO_BOX);
        returShopDetailStatusId.getOptions().add(new String[]{"1", "New"});
        returShopDetailStatusId.getOptions().add(new String[]{"2", "Retur"});
        getFields().add(returShopDetailStatusId);

        returShopDetailDescription = new Field();
        returShopDetailDescription.setName("returShopDetailDescription");
        returShopDetailDescription.setCaption("description");
        returShopDetailDescription.setListProperties(" width=\"70%\"");
        returShopDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        returShopDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        getFields().add(returShopDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(returShopDetailItemId);
        getFieldsAdd().add(returShopDetailSize);
        getFieldsAdd().add(returShopDetailQuantity);
        getFieldsAdd().add(returShopDetailEstimatedDate);
        getFieldsAdd().add(returShopDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(returShopDetailItemId);
        getFieldsEdit().add(returShopDetailSizeId);
        getFieldsEdit().add(returShopDetailQuantity);
        getFieldsEdit().add(returShopDetailEstimatedDate);
        getFieldsEdit().add(returShopDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(returShopDetailItemId);
        getFieldsList().add(returShopDetailSizeId);
        getFieldsList().add(returShopDetailQuantity);
        getFieldsList().add(returShopDetailEstimatedDate);
        getFieldsList().add(returShopDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }
   @Override
    public StringBuffer process() {
        if (getAction().equals(Action.TRANS_DETAIL_ADD)) {
            return AddSize.process(this);
        }
//        if (getAction().equals(Action.TRANS_DETAIL_LIST)) {
//            return ListTransaction.process(this);
//        }
        return super.process();
    }
    @Override
    public String validate(Object data) {
        EntityManager em = getEntityManager();
        PosMasterStatus statusGood = Status.getStatusGood(em);
        PosTransactionReturShopDetail returShopDetail = (PosTransactionReturShopDetail) data;
        PosMasterItemStock stock = Item.getStock(em, returShopDetail.getReturShopDetailItemId(), getCurrentUnit(), statusGood, returShopDetail.getReturShopDetailItemId().getItemColorId(), returShopDetail.getReturShopDetailSizeId());
        if (returShopDetail.getReturShopDetailQuantity().compareTo(stock.getItemStockValue()) > 0) {
            return returShopDetail.getReturShopDetailDescription() + " " + Dictionary.getValue(this, "insufficient unit stock") + ", " + Dictionary.getValue(this, "current unit stock: ") + ": " + stock.getItemStockValue() + " " + Dictionary.getValue(this, "will be retur") + ": " + returShopDetail.getReturShopDetailQuantity();
        }
        return null;
    }
}
